package com.myshoutui;

import java.text.DecimalFormat;
import java.util.ArrayList;

import com.myshout.Client;
import com.myshout.ImageFetcher;
import com.myshout.Store;
import com.myshout.android.commmon.Utils;

import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;

public class SearchAdapter extends BaseAdapter implements ListAdapter {
	
	private Activity mContext = null;
	private ArrayList<Store> mStoreItems=null;
    DecimalFormat df = new DecimalFormat("#.#");

	public SearchAdapter(Activity context,ArrayList<Store> storeList) {
		// TODO Auto-generated constructor stub
		mContext = context;
		mStoreItems=storeList;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mStoreItems.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getItemViewType(int arg0) {
		// TODO Auto-generated method stub
		return 1;
	}



	  private String restrict(String s, int len) {
          return s.length() > (len) ? s.substring(0, len) : s;
      }
	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		

        View row = arg1;
        RowViewCacher wrapper = null;

        if (row == null) {
            LayoutInflater inflater = mContext.getLayoutInflater();
            row = inflater.inflate(R.layout.searchitemrow, null);
            wrapper = new RowViewCacher(row);
            row.setTag(wrapper);
        } else {
            wrapper = (RowViewCacher) row.getTag();
        }

        Store store = mStoreItems.get(arg0);

        double distance = 0.0d;
		if (((MyShoutDashboardActivity)mContext).location != null & store.getLatitude() != null && store.getLongitude() != null) {
            distance = Utils.distanceInMiles(store.getLongitude(), store.getLatitude(), (((MyShoutDashboardActivity)mContext).location).getLongitude(), (((MyShoutDashboardActivity)mContext).location).getLatitude());
            wrapper.getDistance().setText(df.format(distance) + " mi.");
            
            if (store.getDeliveryAvailable() == 1) {
                wrapper.getDistance().setText("Delivers");
            }
        } else {
            wrapper.getDistance().setText(df.format(distance) + " unk.");
        }

        wrapper.getTitle().setText(restrict(store.getName(), 40));
        ((MyShoutDashboardActivity)mContext).loadIcon(wrapper.getIcon(), store);

        StringBuffer sb = new StringBuffer();
        
        if (store.getDescription() != null && !"".equals(store.getDescription())) {
            sb.append(store.getDescription(45));
            sb.append("\n");
        }
        
        if (store.getAddress() != null) {
        if (store.getAddress().length() > 50)
            sb.append(store.getAddress().subSequence(0, 50)).toString().replace("\\n", "");
        else 
            sb.append(store.getAddress());
        }
        
        if (store.getOwner() != null) {
            sb.append("\nAccepts Online Orders");
            row.setBackgroundColor(Color.rgb(0xAD, 0xD8, 0xE6)); // LTBLUE
            wrapper.getTitle().setTextColor(Color.RED);
            wrapper.getDescription().setTextColor(Color.BLACK);
            wrapper.getYelpReviewsCount().setTextColor(Color.BLACK);
            wrapper.getDistance().setTextColor(Color.BLACK);                
        } else {
            row.setBackgroundColor(Color.WHITE);
            wrapper.getTitle().setTextColor(Color.BLACK);
            wrapper.getDescription().setTextColor(Color.BLACK);
            wrapper.getYelpReviewsCount().setTextColor(Color.BLACK);
            wrapper.getDistance().setTextColor(Color.BLACK);
        }

        for(int i=0; i< wrapper.getAllStarViews().size();i++){
        	 wrapper.getAllStarViews().get(i).setVisibility(View.INVISIBLE);
        	 wrapper.getAllStarViews().get(i).setBackgroundResource(R.drawable.ratingstar);

        }
        
        wrapper.getDescription().setText(sb.toString());
        Log.d("Store Desc:", sb.toString());
        
     //   wrapper.getStarsIcon().setImageDrawable((Drawable) yelpStars.get("" + store.getYelpRating()));
        Log.e("", "Rating comming "+ store.getYelpRating() + "size "+ wrapper.getAllStarViews().size());
        if(store.getYelpRating()!=0){
         for(int i=0; i< store.getYelpRating();i++){
         	 wrapper.getAllStarViews().get(i).setVisibility(View.VISIBLE);
         }
        }
        if (store.getYelpNumRatings() > 0)
            wrapper.getYelpReviewsCount().setText("(" + store.getYelpNumRatings() + " reviews)");
        else
            wrapper.getYelpReviewsCount().setText("(Not reviewed yet)");

        return (row);
    
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void registerDataSetObserver(DataSetObserver arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean areAllItemsEnabled() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isEnabled(int position) {
		// TODO Auto-generated method stub
		return false;
	}

}
